var TimelineSlot = Class.create({
    initialize: function(year, term) {
        this.year = year;
        this.term = term;
        this.credits = 0;
        this.grade = 0;
        
        this.display();
    },
    
    update: function() {},
    
    display: function () {
    
        var nextyear = this.year-1999;
        nextyear = nextyear < 10 ? "/0"+nextyear : "/"+nextyear;
    
        var selfDiv = Builder.node('div', {id:'timeline'+this.year+this.term, className: 'timeline-slot'+(this.term?' summer':'')}, [
            Builder.node('p', {className: 'heading'}, (this.term?"SS "+this.year:"WS "+this.year+nextyear))
        ]);
        
        //if (self.term) selfDiv.addClassName('summer');
        
        $("timeline").appendChild(selfDiv);
        
    }
    
});

var Timeline = Class.create({
    initialize: function(startyear, endyear) {
        this.slots = {};
        this.start = startyear;
        this.end = endyear;

        for (var year = endyear; year >= startyear; year--) {
            this.slots['timeline'+year+'0']  = new TimelineSlot(year, 0);
            this.slots['timeline'+year+'1']  = new TimelineSlot(year, 1);
        }

        this.slots['timeline'+(startyear-1)+'0']  = new TimelineSlot(startyear-1, 0);
        
        this.slotArray = Object.keys(this.slots);
        
        $("timeline").style.width = ((1+endyear-startyear)*2*195+195)+"px";
    },
    
    guessStudyStart: function () {
        firsts = 9999;
        firstt = 1;
        for (cid in Globals.courses) {
            if (Globals.courses[cid].instance.year <= firsts) {
                
                if (Globals.courses[cid].instance.term == 1)
                    firsts = Globals.courses[cid].instance.year - 1;    
                else
                    firsts = Globals.courses[cid].instance.year;     
            }
        }
        if (firsts == 9999) {
            firsts = Globals.endYear - 1;
        }
        return firsts;
    },

	switchView: function() {
		if (Globals.view == 'boxes') {
			Globals.view = 'timeline';
			$("menuswitch").innerHTML = "Switch to Module Boxes";
			new Effect.Parallel([
				new Effect.Appear("timeline", {duration: .5}),
				new Effect.Move("timeline", {mode: 'absolute', y: 25, x: 225}),

				new Effect.Fade("module-box", {duration: .5}),
				new Effect.Move("module-box", {mode: 'relative', y: -600})


			]);
		} else if (Globals.view = 'timeline') {
			Globals.view = 'boxes';
			$("menuswitch").innerHTML = "Switch to Timeline";
			new Effect.Parallel([
				new Effect.Move("timeline", {mode: 'absolute', y: 600, x: 225}),
				new Effect.Fade("timeline", {duration: .5}),

				new Effect.Appear("module-box", {duration: .5}),
				new Effect.Move("module-box", {mode: 'relative', y: 600})

			]);
		}
		
	}
});

